from django.db import models
from user.models import *

class FlowConf(models.Model):
    name = models.CharField(max_length=64,verbose_name='工作流名称')
    customfield = models.TextField(verbose_name='自定义字段')
    description = models.TextField(verbose_name='描述')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_flowconf'



class ApproveConf(models.Model):    #审批角色配置
    flowconf = models.ForeignKey(to=FlowConf,on_delete=models.CASCADE)
    sequence = models.IntegerField(verbose_name='审批序号')
    approve_type_choice = (
        ('1','指定人员审批'),
        ('2','角色组审批')
    )
    approve_type_id = models.CharField(choices=approve_type_choice,default='1',max_length=16)
    approve_id = models.CharField(max_length=32,verbose_name='审批ID')

    def __str__(self):
        return self.flowconf.name

    class Meta:
        db_table = 'tb_approveactionconf'


class WorkOrder(models.Model):
    flowconf = models.ForeignKey(to='FlowConf',on_delete=models.CASCADE,verbose_name='工单名称')
    create_user = models.ForeignKey(to=User,on_delete=models.CASCADE,verbose_name='创建者')
    create_ts = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    status_cat = (
        ('0', '审批中'),
        ('1', '被退回'),
        ('2', '完成'),
    )
    order_status = models.CharField(max_length=5, verbose_name='工单状态', choices=status_cat, default='0')
    parameter = models.TextField(verbose_name='工单参数',default={})

    class Meta:
        verbose_name_plural = '实例化工单'

    def __str__(self):
        return self.flowconf.name


class SubOrder(models.Model):
    action_cat = (
        ('0', '待处理'),
        ('1', '通过'),
        ('2', '退回'),
        ('3', '否决'),
    )
    suborder_cat = (
        ('0', '待上一节点处理'),
        ('1', '待处理'),
        ('2', '已经处理'),
    )
    mainorder = models.ForeignKey(WorkOrder,on_delete=models.CASCADE, verbose_name='实例工单名称')
    approve_user = models.ForeignKey(User, verbose_name='审批对象',blank=True, null=True, on_delete=models.SET_NULL)

    # Role-->name
    approve_user_role = models.CharField(verbose_name='审批角色', max_length=50)
    # Role-->id
    approve_userrole_id = models.IntegerField(verbose_name='审批角色ID', null=True, blank=True)

    approve_type_id = models.CharField(max_length=4,verbose_name='审批类型ID')

    # ApproveConf --> sequence：审批序号
    sequence_number = models.IntegerField(verbose_name='审批序号')
    approve_ts = models.DateTimeField(verbose_name='审批时间', auto_now=True)

    action_status = models.CharField(verbose_name='审批状态', choices=action_cat, max_length=10)
    suborder_status = models.CharField(verbose_name='子任务状态', choices=suborder_cat, max_length=20)
    approve_text = models.TextField(verbose_name='审批意见', blank=True)
    #
    # def __str__(self):
    #     return self.mainorder.
    class Meta:
        verbose_name_plural = '子工单实例'